Licensed under the Creative Commons attribution-noncommercial license. Please share and remix noncommercially, mentioning its origin.
CC-BY_NC

This document was produced using pomp2 version 2.0.9.1 and R version 3.5.1.

Introduction

This tutorial aims to help you get started using pomp2 as a suite of tools for analysis of time series data based on stochastic dynamical systems models. First, we give some conceptual background regarding the class of models—partially observed Markov processes (POMPs)—that pomp2 handles. We then discuss some preliminaries: installing the package and so on. Next, we show how to simulate a POMP using pomp2. We then analyze some data using a few different tools. In so doing, we illustrate some of the package’s capabilities by using its algorithms to fit, compare, and criticize the models using pomp2’s algorithms. From time to time, exercises for the reader are given.

Partially observed Markov process (POMP) models

Structure of a POMP

As its name implies pomp2 is focused on partially observed Markov process models. These are also known as state-space models, hidden Markov models, and stochastic dynamical systems models. Such models consist of an unobserved Markov state process, connected to the data via an explicit model of the observation process. We refer to the former as the latent process model (or process model for short) and the latter as the measurement model.

Mathematically, each model is a probability distribution. Let \(Y_n\) denote the measurement process and \(X_n\) the latent state process, then by definition, the process model is determined by the density \(f_{X_n|X_{n-1}}\) and the initial density \(f_{X_0}\). The measurement process is determined by the density \(f_{Y_n|X_n}\). These two submodels determine the full POMP model, i.e., the joint distribution \(f_{X_{0:N},Y_{1:N}}\). If we have a sequence of measurements, \(y^*_{n}\), made at times \(t_n\), \(n=1,\dots,N\), then we think of these data, collectively, as a single realization of the \(Y\) process.


The following schematic shows shows causal relations among the process model, the measurement model, and the data. From the statistical point of view, the key perspective is that the model is, at least hypothetically, the process that generated the data.

Structure of a POMP. Arrows show the direction of causality. The closed loop from the state process to itself indicates the dynamic nature of this Markovian process. Information flow runs in the opposite direction.

Structure of a POMP. Arrows show the direction of causality. The closed loop from the state process to itself indicates the dynamic nature of this Markovian process. Information flow runs in the opposite direction.


Mathematically, a POMP is characterized by two conditions.

  1. The state process, \(X_n\), is Markovian, i.e., \[\mathrm{Prob}[X_n|X_0,\dots,X_{n-1},Y_1,\dots,Y_{n-1}]=\mathrm{Prob}[X_n|X_{n-1}].\]
  2. The measurements, \(Y_n\), depend only on the state at that time: \[\mathrm{Prob}[Y_n|X_0,\dots,X_{n},Y_1,\dots,Y_{n-1}]=\mathrm{Prob}[Y_n|X_{n}],\] for all \(n=1,\dots,N\).

These conditions can be represented schematically by the following diagram, which indicates the direct dependencies among model variables.


**Conditional independence graph of a POMP.**  The latent state $X_n$ at time $t_n$ is conditionally independent of its history given $X_{n-1}$.  The observation $Y_n$ is conditionally independent of all other variables given $X_n$.  The underlying time scale can be taken to be either discrete or continuous, and the observation times need not be equally spaced.

Conditional independence graph of a POMP. The latent state \(X_n\) at time \(t_n\) is conditionally independent of its history given \(X_{n-1}\). The observation \(Y_n\) is conditionally independent of all other variables given \(X_n\). The underlying time scale can be taken to be either discrete or continuous, and the observation times need not be equally spaced.


Basic model components

To implement a POMP model in pomp2, we have to specify the measurement and process distributions. Note however, that, for each of the process and the measurement models there are two distinct operations that we might desire to perform:

  1. we might wish to simulate, i.e., to draw a (pseudo)random sample from the distribution, or
  2. we might wish to evaluate the density itself at given values of \(X_n\) and/or \(Y_n\).

Following the R convention, we refer to the simulation of \(f_{X_n|X_{n-1}}\) as the rprocess component of the POMP model and the evaluation of \(f_{X_n|X_{n-1}}\) as the dprocess component. Similarly, the simulation of \(f_{Y_n|X_n}\) is the rmeasure component while the evaluation of \(f_{Y_n|X_n}\) is the dmeasure component. Finally, we’ll call a simulator of \(f_{X_0}\) the rinit component. Collectively, we’ll refer to these, and other, similarly basic elements of the model, as the model’s basic components.

The plug-and-play property

A method that makes no use of the dprocess component is said to be “plug-and-play” or to have the “plug-and-play property”. At present, pomp2 is focused on such methods, so there is no reason to discuss the dprocess component further in this document. In the following, we will illustrate and explain how one specifies the rprocess, rmeasure, and dmeasure components of a model in pomp2. We will illustrate this using some simple examples.

Preliminaries

Installing the package

To get started, we must install pomp2, if it is not already installed. This package cannot yet be downloaded from CRAN (though that will change in the near future). However, the latest version is always available at the package homepage on Github. See the package website for installation instructions.

Important information for Windows and Mac users.

In this document, we will ultimately learn to implement POMP models using the package’s “C snippet” facility. This allows the user to write model components using snippets of C code, which is then compiled and linked into a running R session. This typically affords a manyfold increase in computation time. It is possible to avoid C snippets entirely by writing model components as R functions, and we will begin by doing so, but the speed-ups afforded by C snippets are typically too good to pass up. To use C snippets, you must be able to compile C codes. Compilers are not by default installed on Windows or Mac systems, so users of such systems must do a bit more work to make use of pomp2’s facilities. The installation instructions on the package website give details.

Simulation of a POMP

Having dispensed with the preliminaries, we now explore some of the functionality provided by pomp2. To assist the reader in following this exploration, the R codes for this document are available.

The latent state process

Let us see how to implement a very simple POMP model. In particular, let’s begin by focusing on the famous Ricker model (Ricker 1954), which posits a nonlinear relationship between the size, \(N(t)\), of a population in year \(t\) and its size, \(N(t+1)\), one year later: \[N(t+1)=r\,N(t)\,\exp\left(1-\frac{N(t)}{K}\right).\tag{1}\] Here, \(r\) and \(K\) are constant parameters, usually termed the intrinsic growth rate and the carrying capacity, respectively. As written, this is a deterministic model: it does not allow for any variability in the population dynamics. Let’s modify the Ricker equation by assuming that \(r\) is not constant, but instead has random variation from year to year. If we assume that the intrinsic growth rate varies from year to year as a lognormal random variable, we obtain \[N(t+1)=r\,N(t)\,\exp\left(1-\frac{N(t)}{K}+\varepsilon(t)\right),\tag{2}\] where \(\varepsilon(t)\sim\mathrm{Normal}(0,\sigma)\). Note that we’ve introduced a new parameter, \(\sigma\), which quantifies the intensity of the noise in the population dynamics. Ecologically speaking, Eq. 2 is a model with environmental stochasticity.

Typically, it is relatively straightforward to simulate a POMP model. To accomplish this in pomp2, as we’ve already discussed, we specify the rprocess component of the model. We’ll also need to choose values for the model parameters, \(r\), \(K\), and \(\sigma\). We’ll also need to make a choice regarding the initial condition, \(N(0)\). The simplest choice is to treat \(N(0)=N_0\) as a parameter.

library(pomp2)

simulate(t0=0, times=1:20,
  params=c(r=1.2,K=200,sigma=0.1,N_0=50),
  rinit=function (N_0, ...) {
    c(N=N_0)
  },
  rprocess=discrete_time(
    function (N, r, K, sigma, ...) {
      eps <- rnorm(n=1,mean=0,sd=sigma)
      c(N=r*N*exp(1-N/K+eps))
    },
    delta.t=1
  )
) -> sim1
## Warning: 'rmeasure' unspecified: NAs generated.

Notice that we’ve specified the rinit and rprocess components of the model as functions. These functions take as arguments the relevant variables (whether these are state variables or parameters). Importantly, they return named numeric vectors. Names of variables and parameters are very important in pomp2. Notice too that we’ve used the discrete_time function, which encodes the fact that our Ricker model is a discrete-time stochastic process (a Markov chain). The first argument of discrete_time is an R function encoding Eq. 2; the second argument specifies the discrete time-step.

Note also that the parameters are furnished in the form of a named vector, and that we’ve specified both t0 and times. The former is the initial time, \(t_0\), i.e., the time at which the initial conditions apply. Since our initial condition is that \(N(0)=N_0\), our initial time is \(t_0=0\). The times argument specifies the observation times \(t_1,\dots,t_N\).

What sort of an object is sim1? If we print it, we see

sim1
## <object of class 'pomp'>

sim1 is evidently an object of class ‘pomp’. We refer to these as ‘pomp’ objects.

To get more insight into the structure of sim1, we can use spy:

spy(sim1)

pomp2 provides methods for plotting ‘pomp’ objects. For example,

plot(sim1)

We can also recast a ‘pomp’ object as a data frame:

as(sim1,"data.frame")

Casting the ‘pomp’ object as a data frame allows us to use ggplot2 graphics:

  ggplot(data=as.data.frame(sim1),aes(x=time,y=N))+
  geom_line()

Now notice that we had a warning when we ran simulate. The warning was telling us that simulate could not simulate from the measurement process because we had supplied it with no rmeasure component. Let’s see how we can specify rmeasure, the measurement model simulator.

The measurement model

Let’s suppose that non-lethal traps are used to sample the population to determine its size. Each year, some number of traps are set out and \(Y_t\) is the number of animals captured. We might posit \[Y_t\;\sim\;\mathrm{Poisson}(b\,N(t)),\] where the parameter \(b\) is proportional to sampling effort, e.g., the number of traps. This is a measurement model, and we can implement a simulator for it by specifying another function:

simulate(t0=0, times=1:20,
  params=c(r=1.2,K=200,sigma=0.1,N_0=50,b=0.05),
  rinit=function (N_0, ...) {
    c(N=N_0)
  },
  rprocess=discrete_time(
    function (N, r, K, sigma, ...) {
      eps <- rnorm(n=1,mean=0,sd=sigma)
      c(N=r*N*exp(1-N/K+eps))
    },
    delta.t=1
  ),
  rmeasure=function (N, b, ...) {
    c(Y=rpois(n=1,lambda=b*N))
  }
) -> sim2

Note that, again, the rmeasure function need take only the necessary arguments (and ...) and must return a named numeric vector.

Now, in the preceding chunk of code where we construct sim2, there was some redundancy with our earlier construction of sim1. In particular, we specified the same values of t0, times, rinit, and rprocess as before. Since these specifications were stored in sim1, however, we could have simply added in just the new pieces. For example,

simulate(
  sim1,
  params=c(r=1.2,K=200,sigma=0.1,N_0=50,b=0.05),
  rmeasure=function (N, b, ...) {
    c(Y=rpois(n=1,lambda=b*N))
  }
) -> sim2

As before, we can examine our handiwork:

spy(sim2)
as(sim2,"data.frame")
plot(sim2)

ggplot(data=gather(
  as(sim2,"data.frame"),
  variable,value,-time),
  aes(x=time,y=value,color=variable))+
  geom_line()

Notice that now our simulate call produces samples from both the \(N\) and the \(Y\) distributions. simulate will try to sample from the joint distribution of latent states and observables, but will sample from just the latent state process if the rmeasure component is undefined.

Multiple simulations

We can run multiple simulations using the same parameters:

simulate(sim2,nsim=20) -> sims

ggplot(data=gather(
  as.data.frame(sims),
  variable,value,Y,N
),
  aes(x=time,y=value,color=variable,
    group=interaction(.id,variable)))+
  geom_line()+
  facet_grid(variable~.,scales="free_y")+
  labs(y="",color="")

We can also run a single simulation from multiple parameters:

p <- parmat(coef(sim2),3)
p["sigma",] <- c(0.05,0.25,1)
colnames(p) <- LETTERS[1:3]

simulate(sim2,params=p,format="data.frame") -> sims
sims <- gather(sims,variable,value,Y,N)
ggplot(data=sims,aes(x=time,y=value,color=variable,
  group=interaction(.id,variable)))+
  geom_line()+
  scale_y_log10()+
  expand_limits(y=1)+
  facet_grid(variable~.id,scales="free_y")+
  labs(y="",color="")

In the above, coef extracts the parameter vector stored in sim2. parmat makes a matrix with columns that are copies this vector. The second line modifies this matrix so that the columns are a set of points in parameter space that lie along a line with increasing values of the \(\sigma\) parameter.

We can even run multiple simulations at each one of a set of parameters:

simulate(sim2,params=p,
  times=seq(0,3),
  nsim=500,format="data.frame") -> sims
ggplot(data=separate(sims,.id,c("parset","rep")),
  aes(x=N,fill=parset,group=parset,color=parset))+
  geom_density(alpha=0.5)+
  # geom_histogram(aes(y=..density..),position="dodge")+
  facet_grid(time~.,labeller=label_both,scales="free_y")+
  lims(x=c(NA,1000))

Note on pomp2 syntax

pomp2 uses certain syntactic conventions. For example, if x is an object and f is a pomp2 function, then typically f(x,...) -> y yields an object, y, that contains x plus the results of the f operation, together with additional information pertinent to what f did. Here, ... stands for additional arguments to f. Then, if we call f(y), this operation will perform some kind of repeat of the original operation: though the details will vary with f and y, choices made in the original computation (on x) will typically be respected. To change some of these choices, one can do f(y,...), where the ... stands for options of f that correspond to the new choices.

Morever, since y contains so much information, this information will typically be reused when we apply a different function, g, to y, when it makes sense to do so.

The magrittr package (part of the tidyverse), provides new syntax that is especially convenient for us. It allows us to construct pipelines of R commands, which leads (once one is used to it) to code that is easier to read. In brief, a command in ordinary R style, such as

h(g(f(x,...),...),...)

where f, g, h are R functions, x is some R object, and ... represents additional arguments to each of the functions, must be read from the inside out, which is at odds with the sequential nature of the fgh computation. An alternative, of course, is to write something like

y <- f(x,...)
z <- g(y,...)
w <- h(z,...)

which multiplies the entities (y, z, w) one must name and keep track of. In contrast, the pipeline syntax of magrittr allows us to write

x %>% f(...) %>% g(...) %>% h(...)

The object-oriented structure of pomp2 makes this kind of programming quite natural.

Some experienced R programmers find the pipeline syntax uncomfortable or unnecessary at first, and debugging pipelined code requires a somewhat different approach. Of course, it is not necessary to adopt this style of programming to use pomp2, but it is quite natural and, experience shows, quite addictive!

We will use pipelines freely for the remainder of the document. Note that it is not necessary to load magrittr to use this syntax: the %>% operator is imported from magrittr and re-exported by pomp2.

Important note: If you opt to load the tidyverse (or individual packages therein), be sure to load pomp2 after loading these packages. There are some name conflicts between the packages that would otherwise cause pomp2 functions to be masked.


Exercise

Modify the sim2 object constructed above to change the measurement model. In particular, assume that \[Y_t\;\sim\;\mathrm{NegBin}(b\,N(t),\theta),\] where \(b\) and \(\theta\) are parameters. (By this notation, we understand that \(Y_t\) is negative binomially distributed with mean \(b\,N(t)\) and variance \(b\,N(t)+(b\,N(t))^2/\theta\).) Make and plot some simulations for \(t=20,\dots,50\) with different values of the \(\theta\) parameter. [Hint: use the rnbinom function with the mu, size parameterization.]


Parus major population abundance data

We will illustrate some of the pomp2 data-analysis algorithms by performing a limited analysis on a set of bird abundance data. The data are from annual censuses of a population of Parus major (Great Tit) in Wytham Wood, Oxfordshire. They were retrieved as dataset #10163 from the Global Population Dynamics Database version 2 (NERC Centre for Population Biology, Imperial College, 2010). The original source is McCleery and Perrins (1991). They are provided as part of the package, in the data frame parus. Here we display the data graphically and in a table:

parus %>%
  ggplot(aes(x=year,y=pop))+
  geom_line()+geom_point()+
  expand_limits(y=0)

parus

The basic data-type provided by pomp2—the ‘pomp’ object—is designed as a container for a model and data. Let’s construct such an object with these data, together with the Ricker model we’ve already implemented. We do this with a call to the constructor function pomp:

parus %>%
  pomp(
    times="year", t0=1960,
    rinit=function (N_0, ...) {
      c(N=N_0)
    },
    rprocess=discrete_time(
      function (N, r, K, sigma, ...) {
        eps <- rnorm(n=1,mean=0,sd=sigma)
        c(N=r*N*exp(1-N/K+eps))
      },
      delta.t=1
    ),
    rmeasure=function (N, b, ...) {
      c(pop=rpois(n=1,lambda=b*N))
    }
  ) -> rick

Notice that the measurement model simulator (rmeasure) is not the same as before, reflecting the fact that the data are named pop, not Y as before. Also notice that we specify the time variable by name, thus allowing the data to dictate the observation times. The t0 argument specifies that the stochastic latent state process is initialized in year 1960.

Continuous-time process models

We’ve already shown how to implement a discrete-time model; let’s now see how to implement a continuous-time model. We’ll implement a very simple model of stable but stochastic population dynamics, the logistic, or Verhulst-Pearl, equation with environmental stochasticity. We’ll write this model as a stochastic differential equation (SDE), specifically an Itô diffusion: \[dN = r\,N\,\left(1-\frac{N}{K}\right)\,dt+\sigma\,N\,dW(t),\tag{3}\] where \(N\) is the population size, \(r\) is a fixed rate, the so-called “Malthusian parameter”, \(K\) is the population’s “carrying capacity”, \(\sigma\) describes the intensity of extrinsic stochastic forcing on the system, and \(dW\) is an increment of a standard Wiener process. [Those unfamiliar with Wiener processes and Itô diffusions will not go far wrong thinking of \(dW(t)\), for each time \(t\), as a normal random variable with mean zero and standard deviation \(\sqrt{dt}\).] To implement this model in pomp2, we must tell the package how to simulate this model. The easiest way to simulate such an SDE is via the Euler-Maruyama method. In this approximation, we take a large number of very small steps, each of duration \(\Delta t\). At each step, we hold the right-hand side of the above equation constant, compute \(\Delta N\) using that equation, and increment \(N\) accordingly. pomp2 gives us the euler function to assist us in implementing the Euler-Maruyama method. To use it, we must encode the computations that take a single step. As before, we can do so by writing a function.

vpstep <- function (N, r, K, sigma, delta.t, ...) {
  dW <- rnorm(n=1,mean=0,sd=sqrt(delta.t))
  c(N = N + r*N*(1-N/K)*delta.t + sigma*N*dW)
}

This function computes the value of \(N(t+\Delta t)\) given the value of \(N(t)\), \(\Delta t\), and the parameters.

We fold this with the data into a ‘pomp’ object via a call to the ‘pomp’-object constructor function, pomp. We’ll also include the same measurement model we used before. Because everything but the ‘rprocess’ component is the same as with the Ricker model, to accomplish this, we can simply do

rick %>% pomp(rprocess=euler(vpstep,delta.t=1/365)) -> vp

Notice that we’ve specified an Euler-Maruyama step of about 1 day: it will take 365 of these steps to get us from one observation to the next.

As before, we can plot this ‘pomp’ object, recast it as a data frame, and simulate it for any given choice of the parameters.


Exercise

The following codes produce several simulations for parameters \(r=0.5\), \(K=2000\), \(\sigma=0.1\) and \(b=0.1\), and plot them on the same axes as the data. Notice that the format="data.frame" and include.data=TRUE options facilitate this. Vary the parameters to try to achieve a better fit to the data, as judged purely “by eye”.

vp %>%
  simulate(
    params=c(r=0.5,K=2000,sigma=0.1,b=0.1,N_0=2000),
    format="data.frame", include.data=TRUE, nsim=5) %>%
  mutate(ds=case_when(.id=="data"~"data",TRUE~"simulation")) %>%
  ggplot(aes(x=year,y=pop,group=.id,color=ds))+
  geom_line()+
  labs(color="")


C snippets

To this point, we’ve implemented two models by specifying their basic components as R functions. While this has the virtue of transparency, it puts severe constraints on computational performance due to intrinsic limits in the speed with which R codes can be interpreted. If all we want to do is run a few simulations, this is not a problem. As we’ll see, however, in attempting to perform parameter estimation and other inferences, we will need all the speed we can readily get. We achieve potentially massive speed-ups by implementing our basic model components in a language that can be compiled. pomp2 makes this easy. The key innovation is the C snippet. Let’s see how to code up the Ricker and Verhulst-Pearl models using C snippets. First, we’ll code up the rinit and rmeasure components, which the two models share.

Csnippet("
  pop = rpois(b*N);  
  ") -> rmeas

Csnippet("
  N = N_0;
  ") -> rinit

These are about as simple as one can get. As the name suggests, each is just a snippet of C code: not all variables are declared (in fact, in these examples, none are) and the context of the snippet is not specified. In particular, these snippets are not actually complete C functions, so by themselves, they cannot be compiled. They must not actually violate the rules of C syntax however. Among other things, lines must end with a semicolon (;), variable names must respect C restrictions, etc. Although one can enclose essentially arbitrarily complex C code in a C snippet, one can do quite a lot with very simple snippets. If you are new (even very new) to C, don’t worry: once you master a few simple rules, you’ll be able to code up C snippets just as easily, or even more easily, than you code up R functions and you’ll learn to value the resulting speed-up extremely.

Now we’ll code the Ricker and Verhulst-Pearl simulation steps as C snippets.

Csnippet("
  double eps = rnorm(0,sigma);
  N = r*N*exp(1-N/K+eps);
") -> rickstepC

Csnippet("
  double dW = rnorm(0,sqrt(dt));
  N += r*N*(1-N/K)*dt+sigma*N*dW;
") -> vpstepC

A few observations are in order. First, note that the local variables eps and dW are declared “double”, the standard C data-type for floating point numbers. Observe that dt is a variable that is defined in the context of the vpstepC snippet; when this snippet is executed, dt will be provided by pomp and will be equal to the size of the Euler step actually being taken. Note also that in each of these snippets, the value of N gets over-written by its new value at the next time-step. This is the goal of the C snippets we supply to specify the ‘rprocess’ component of a model. Finally, notice that neither the state variable N nor any of the parameters are declared. The declarations will be handled in a different way, as we’ll see in a moment.

When furnished with one or more C snippets, pomp will provide the necessary declarations and context, compile the resulting C code, dynamically link to it, and use it whenever the corresponding basic model component is needed. We cause all this to happen when we construct an object of class pomp via a call to the constructor function. Let’s do this for the two models now.

parus %>%
  pomp(
    times="year", t0=1960,
    rinit=rinit,
    rmeasure=rmeas,
    rprocess=discrete_time(rickstepC,delta.t=1),
    statenames="N",
    paramnames=c("r","K","sigma","b","N_0")
  ) -> rickC
parus %>%
  pomp(
    times="year", t0=1960,
    rinit=rinit,
    rmeasure=rmeas,
    rprocess=euler(vpstepC,delta.t=1/365),
    statenames="N",
    paramnames=c("r","K","sigma","b","N_0")
  ) -> vpC

In these calls, we use the statenames and paramnames arguments to indicate which of the undeclared variables in the C snippets rickstepC and vpstepC are state variables and which are fixed parameters. Since dW and eps are declared as local variables within the C snippets themselves, we don’t need to mention them here. The rnorm and rpois functions are part of the R API: see the manual on “Writing R Extensions” for a description of these and the other distribution functions provided as part of the R API. A full set of rules for writing pomp2 C snippets is given in the package help (?Csnippet).


Exercise

Using the ‘pomp’ objects just constructed, explore model simulations at a variety of different parameters. As before, plot simulations and data on the same axes for comparison purposes.


Exercise

Write a C snippet implementing the negative binomial measurement model you explored previously. Fold it into the ‘pomp’ objects just constructed. Remember, there is no need to re-specify components you have already specified: by calling pomp on a ‘pomp’ object you can modify some or all of the basic model components. Plot simulations and data on the same axes, as in the immediately preceding exercise.


The dmeasure component

As mentioned in the introduction, the dmeasure component is the other side of the rmeasure component. The latter simulates the measurement model whereas the former evaluates the measurement model’s probability density function. The following C snippet encodes the dmeasure component.

Csnippet("
  lik = dpois(pop,b*N,give_log);
") -> dmeas

Here, dpois again comes from the R API. It takes three arguments, the datum (pop), the Poisson parameter (b*N), and give_log. When give_log=0, dpois returns the Poisson likelihood; when give_log=1, dpois returns the log of this likelihood. When this snippet is executed, pomp will provide the value of give_log according to its needs. It is the user’s responsibility to make sure that the correct value is returned for both possible values of give_log. This is one of the most common places where newbies make mistakes!


Exercise

Write the dmeasure component for your negative binomial model both as an R function and as a C snippet.


The particle filter

We are now in a position to be able to compute the likelihood of the data given any set of parameters for either of our models. For this purpose, we use the particle filter. This powerful algorithm is at the heart of several of pomp2’s inference methods. We won’t describe the theory of the particle filter here. The tutorial by Arulampalam et al. (2002) explains the theory in an accessible way. The pomp2 Journal of Statistical Software paper gives pseudocode and some examples. The pomp2 documentation page lists several other tutorial documents that go into more detail.

In pomp2, the simplest version of the particle filter is implemented in the function pfilter. Its only required arguments are the ‘pomp’ object and the number of particles, i.e., the Monte Carlo sample size.

rickC %>%
  pfilter(Np=1000,
    params=c(r=1.2,K=2000,sigma=0.3,N_0=1600,b=0.1),
    dmeasure=dmeas,
    paramnames="b",statenames="N") -> pfrick

Notice that, in this call, we specified the dmeasure component using the C snippet we wrote above. What would have happened had we not specified this?

Notice that, because we here introduced a new C snippet, we again had to indicate which of the undeclared variables in dmeas are parameters and which are latent state variables.

What kind of object is pfrick?

pfrick
## <object of class 'pfilterd_pomp'>

As a ‘pfilterd_pomp’ object, pfrick contains rickC plus a wealth of information regarding the particle filter operation that created it. For example, we can extract the estimated log likelihood at these (arbitrarily chosen) parameters:

logLik(pfrick)
## [1] -148.7142

There is also a plot method for ‘pfilterd_pomp’ objects and one for coercing them to data frames.

plot(pfrick)

as(pfrick,"data.frame")

Both of these reveal that pfrick contains information about the effective sample size of the particle filter (ess) and the conditional log likelihood, cond.loglik, which in the notation of the introduction, is \[\log f_{Y_n|Y_{1:n-1}}(y_n^*|y_{1:n-1}^*).\]

The particle filter is a Monte Carlo algorithm. Accordingly, it gives us only a noisy estimate of the likelihood. We can reduce this noise by increasing the number of particles, Np, and we can estimate the magnitude of the Monte Carlo error by running a few independent particle filters. For example:

replicate(10, pfrick %>% pfilter() %>% logLik()) -> lls
lls
##  [1] -148.4392 -148.3138 -148.5999 -148.2257 -148.9847 -148.3967 -148.5985
##  [8] -148.5690 -149.2660 -148.2189
logmeanexp(lls,se=TRUE) -> ll_rick1
ll_rick1
##                        se 
## -148.5159675    0.0899921

In the first line, notice that we did not need to specify Np, despite the fact that there is no default value of this parameter. Indeed, because pfrick is a ‘pfilterd_pomp’ object, it knows the value of Np that was used in its own creation. By default, this same value is used again when it is passed to pfilter. We could, of course, have used a different value simply by specifying Np in this call to pfilter.

The last line of the preceding code chunk computes the log of the mean of the estimated likelihoods and the standard error on this mean via the delta method. Since the particle filter gives an unbiased estimate of the likelihood (not the log likelihood), this operation is sensible, provided the Monte Carlo error is not too large.

Let’s repeat the operation for the Verhulst-Pearl model, again at arbitrary parameters.

replicate(10,
  vpC %>%
    pfilter(Np=1000,dmeasure=dmeas,
      params=c(r=0.5,K=2000,sigma=0.1,b=0.1,N_0=2000),
      paramnames="b",statenames="N") %>%
    logLik()) %>%
  logmeanexp(se=TRUE) -> ll_vp1
ll_vp1
##                        se 
## -186.1256268    0.3904347

Exercise

Compute the likelihood for the parameters you found in your attempt to estimate parameters “by eye”.


Trajectory matching

Trajectory matching is the method of estimating the parameters of a deterministic model by fitting the model to data assuming independent measurement errors. Although pomp2‘s main focus is on stochastic models, it does provide facilities for trajectory matching. pomp2 makes a conceptual distinction between the stochastic process and a deterministic skeleton, which we can view as a deterministic model related to the stochastic process’ central tendency. We’ll not go into mathematical details here: instead, we’ll illustrate with two examples.

A discrete-time deterministic skeleton

A deterministic skeleton of the stochastic Ricker model is the Ricker map, Eq. 1. We implement this for pomp2, again either as an R function or a C snippet and pass it to pomp2 functions via the skeleton argument. For example:

rickC %>%
  pomp(
    skeleton=map(
      Csnippet("DN = r*N*exp(1-N/K);"),
      delta.t=1
    ),
    paramnames=c("r","K"), statenames="N"
  ) -> rickC

Here, the left-hand side of Eq. 1 is indicated by the D prefix: in skeleton snippets, we don’t over-write the state variable N. We indicate that the skeleton is a discrete-time map using the map function.

A continuous-time deterministic skeleton

A deterministic skeleton of the Verhulst-Pearl model is the vectorfield (or ordinary differential equation), \[\frac{dN}{dt} = r\,N\,\left(1-\frac{N}{K}\right).\] We fold this into the vpC ‘pomp’ object so:

vpC %>%
  pomp(
    skeleton=vectorfield(Csnippet("DN = r*N*(1-N/K);")),
    paramnames=c("r","K"), statenames="N"
  ) -> vpC

Since the skeleton here is a vectorfield, in this C snippet, DN is filled with the value of the time-derivative of N. See the package help (?Csnippet) for a complete set of rules for writing C snippets.

Trajectories of the deterministic skeleton

With the deterministic skeleton in place we can generate trajectories of the skeleton using trajectory. For example:

p <- parmat(c(r=0.5,K=2000,sigma=0.1,b=0.1,N_0=2000),10)
p["N_0",] <- seq(10,3000,length=10)
vpC %>%
  trajectory(params=p,format="data.frame") %>%
  ggplot(mapping=aes(x=year,y=N,color=.id,group=.id))+
  guides(color=FALSE)+
  geom_line()+
  theme_bw()

As with simulate, one can use trajectory to compute multiple trajectories at once, for varying values of the parameters.

Parameter estimation using trajectory matching

In pomp2, the function traj_objfun constructs an objective function quantifying the mismatch between model predictions and data. For this purpose, it uses the dmeasure component of the model. This function can be given to any of the large variety of numerical optimizers available in R and R packages. These optimizers search parameter space to find parameters under which the likelihood of the data, given a trajectory of the deterministic skeleton, is maximized.

We’ll demonstrate using the Verhulst-Pearl model.

vpC %>%
  traj_objfun(
    est=c("K","N_0"),
    params=c(r=0.5,K=2000,sigma=0.1,b=0.1,N_0=2000),
    dmeasure=dmeas, statenames="N", paramnames="b"
  ) -> ofun

This invocation of traj_objfun creates an objective function, ofun, that can be used to estimate the three parameters \(K\) and \(N_0\). It will hold the other three parameters, \(r\), \(\sigma\), and \(b\), fixed at the values they are given in params.

Notice that, in this code chunk, we had to specify dmeasure once again. Why? What would have happened had we not done so?

What kind of object is ofun?

ofun
## <object of class 'traj_match_objfun'>

‘traj_match_objfun’ objects, like the objective functions created by the pomp2 functions spect_objfun, probe_objfun, and nlf_objfun, is an R function, but it is also more than an R function. It contains not only the vpC ‘pomp’ object, but it additionally saves information each time it is evaluated. Let’s see how such stateful objective functions make it easy to use a wide range of numerical optimization routines.

We can evaluate ofun at any point in the 2-dimensional \(K\)-\(N_0\) space. For example:

ofun(c(1000,3000))
## [1] 1161.543

The value returned by ofun is the negative log likelihood, as returned by the model’s dmeasure component. We can estimate the parameters using, for example, the subplex algorithm implemented in the subplex package:

library(subplex)

subplex(c(2000,1500),fn=ofun) -> fit
fit 
## $par
## [1] 1968.474 1895.245
## 
## $value
## [1] 276.2893
## 
## $counts
## [1] 335
## 
## $convergence
## [1] 0
## 
## $message
## [1] "success! tolerance satisfied"
## 
## $hessian
## NULL

Note that fit contains, among other things, the estimated parameters (element par) and the minimized value of the negative log likelihood (value).

To make absolutely certain that ofun remembers these estimates, we evaluate it once at fit$par:

ofun(fit$par)
## [1] 276.2893
coef(ofun)
##        r        K    sigma        b      N_0 
##    0.500 1968.474    0.100    0.100 1895.245
logLik(ofun)
## [1] -276.2893

Then we can, for example, extract the fitted trajectory thus:

ofun %>%
  trajectory(format="data.frame") %>%
  ggplot(mapping=aes(x=year,y=N,color=.id,group=.id))+
  guides(color=FALSE)+
  geom_line()+
  theme_bw()

We can superimpose the model predictions on the data as follows.

ofun %>%
  trajectory(format="data.frame") %>%
  mutate(
    pop=coef(ofun,"b")*N,
    .id="prediction"
  ) %>%
  select(-N) %>%
  rbind(
    parus %>%
      mutate(
        .id="data",
        pop=as.double(pop)
      )
  ) %>%
  spread(.id,pop) %>%
  ggplot(aes(x=year))+
  geom_line(aes(y=prediction))+
  geom_point(aes(y=data))+
  expand_limits(y=0)+
  labs(y="pop")

Parameter transformations

Very commonly, model parameters must obey certain constraints. For example, the parameters in the two models we’ve looked at so far are all constrained to be positive. In estimating parameters, however, one frequently wants to employ a numerical optimization method that does not respect constraints. One way of accomodating such unconstrained optimizers is to transform the parameter space so that the constraints disappear. For example, by log-transforming the \(r\) parameter in the Verhulst-Pearl model (Eq. 3), one obtains the superficially different equation \[dN = e^\rho\,N\,\left(1-\frac{N}{K}\right)\,dt+\sigma\,N\,dW(t),\] where \(\rho=\log{r}\). In this model, \(\rho\) can take any (positive or negative) values while \(r\) remains positive.

We incorporate parameter transformations using the partrans argument to many pomp2 functions, specifying them using the parameter_trans function. In general, the parameter transformations, like other basic model components, can be supplied using R functions or C snippets. If we are merely log-transforming, logit-transforming, or log-barycentric-transforming parameters, however, it is even easier. The following code chunk implements log transformation of the all the parameters of the Verhulst-Pearl and Ricker models.

vpr_partrans <- parameter_trans(log=c("r","K","sigma","b","N_0"))

We could then provide vpr_partrans as needed to any of the various pomp2 inference methods, via the partrans argument. For example, to estimate parameters for the Verhulst-Pearl model on the transformed scale, we do

vpC %>%
  traj_objfun(
    est=c("b","K","N_0"),
    params=c(r=0.5,K=2000,sigma=0.1,b=0.1,N_0=2000),
    partrans=parameter_trans(log=c("K","N_0","b")),
    dmeasure=dmeas, statenames="N", paramnames=c("b","K","N_0")
  ) -> ofun2

subplex(log(c(0.1,2000,1500)),fn=ofun2) -> fit
ofun2(fit$par)
## [1] 276.2893
coef(ofun2)
##             r             K         sigma             b           N_0 
##    0.50000000 2506.85235680    0.10000000    0.07852373 2413.59561601

Exercise

Estimate the parameters for the Verhulst-Pearl model assuming negative binomial errors. Do not attempt, at first, to estimate all parameters simultaneously. Focus on estimating \(K\), \(N_0\) and the parameters of the measurement model. It will probably be helpful to make use of parameter transformations to enforce the model constraints.


Exercise

Estimate some or all of the parameters of the Ricker model using trajectory matching. It is probably a good idea to use parameter transformations.


Maximizing the likelihood by iterated filtering

Let us now turn to the main focus of the pomp2 package: parameter estimation for fully stochastic models. Iterated filtering is a method for maximizing the likelihood. The method was introduced in its original form by Ionides et al. (2006), and was subsequently much improved by Ionides et al. (2015). The latter paper rigorously expounds the theory. An addendum to the pomp *Journal of Statistical Software paper provides pseudocode and a simple example. Finally, a tutorial on the theory and practice is linked from the pomp2 documentation index. Here, we confine ourselves to demonstrating how the IF2 algorithm (Ionides et al. 2015) is applied to the toy examples we have been discussing.

Estimating the log likelihood

Now, although we have observed the intended improvement in the log likelihood, we should be careful to note that the log likelihood displayed in this plot is the log likelihood of the perturbed model. This model differs from the one we are interested in. To compute the likelihood of our focal model at the parameter returned by mif2, we need to perform a few particle filter operations:

replicate(5, mf1 %>% pfilter() %>% logLik()) %>% logmeanexp(se=TRUE)
##                        se 
## -148.0788938    0.1434606

Note regarding parallelization

The above foreach call, as written, will result in serial, not parallel, execution of the 100 computations. To actually parallelize, one must register a parallel backend. A good first choice is doParallel, which is a separate package that you must explicitly load. The following chunk of code shows how to do this, and prints the number of “workers” that result.

library(doParallel)
registerDoParallel()
getDoParWorkers()

doParallel allows one to exploit multiple cores on a single machine, or to expand a parallel computation across a cluster.


Exercise

Use doParallel to parallelize the estimation of likelihood for your negative-binomial model.


More search effort

At this point, there’s no particular reason to suspect that our IF2 searches have arrived at their destination. In general, it is hard to know a priori how much effort will be required to find the MLE. Let’s continue the search, starting with the best points we’ve uncovered so far.

estimates %>%
  filter(!is.na(loglik)) %>%
  filter(loglik > max(loglik)-30) %>%
  select(-loglik,-loglik.se) -> starts

foreach (start=iter(starts,"row"),
  .combine=rbind, .packages=c("pomp2"), 
  .errorhandling="remove", .inorder=FALSE) %dopar% {
    
    mf1 %>% 
      mif2(params=start) %>%
      mif2() -> mf
    
    replicate(5, 
      mf %>% pfilter() %>% logLik()
    ) %>%
      logmeanexp(se=TRUE) -> ll
    
    data.frame(as.list(coef(mf)),loglik=ll[1],loglik.se=ll[2])
    
  } -> ests1

Note that, in the above, we’ve performed a total of 100 mif2 iterations per starting point. The code above also does the post-mif2 likelihood estimation. It returns just the parameter and likelihood estimates.

We can combine the new estimates with the old ones into a general database:

estimates %>%
  rbind(ests1) -> estimates

estimates%>%
  arrange(-loglik)
estimates %>%
  filter(loglik>max(loglik,na.rm=TRUE)-4) %>%
  do({
    pairs(~loglik+r+K+sigma+N_0,data=.,
      pch=16,
      col=if_else(is.na(.$loglik),"#99999955","#ff0000ff"))
    .
  }) %>% 
  invisible()

In this plot, we begin to see the emergence of structure in the likelihood surface. In particular, what looks like a ridge of high likelihood is visible in the \(r\)-\(\sigma\) projection. Such structures are very interesting in that they contain clues as to the manner in which the model is fitting the data. They can also pose challenges to efficient estimation, since climbing up to a ridge is harder than traversing it.

Likelihood profile

We can improve the quality of our estimates and obtain likelihood-ratio-test-based confidence intervals by constructing profile likelihoods. In a likelihood profile, one varies the focal parameter (or parameters) across some range, maximizing the likelihood over the remaining parameters at each value of the focal parameter. The following codes construct a likelihood profile over \(r\).

estimates %>%
  filter(loglik>max(loglik)-10) %>%
  select(r,K,sigma,N_0,b) %>%
  apply(2,range) -> ranges
ranges
##                r        K     sigma      N_0 b
## [1,]  0.07189329 177.2265 0.3014316 134.2206 1
## [2,] 18.49198361 424.3266 1.5890275 161.9926 1
profileDesign(
  r=10^seq(
    from=log10(ranges[1,1]),
    to=log10(ranges[2,1]),
    length=20
  ),
  lower=ranges[1,-1],
  upper=ranges[2,-1],
  nprof=50
) -> starts

dim(starts)
## [1] 1000    5
pairs(~r+K+sigma+N_0+b,data=starts)

foreach (start=iter(starts,"row"),
  .combine=rbind, .packages=c("pomp2"),
  .errorhandling="remove", .inorder=FALSE) %dopar% {
  
  mf1 %>%
    mif2(
      params=start,
      partrans=parameter_trans(log=c("K","sigma","N_0")),
      rw.sd=rw.sd(K=0.02,sigma=0.02,N_0=ivp(0.02)),
      paramnames=c("K","sigma","N_0","b")
    ) %>%
    mif2() -> mf
  
  replicate(5, 
    mf %>% pfilter() %>% logLik()
  ) %>%
    logmeanexp(se=TRUE) -> ll
  
  data.frame(as.list(coef(mf)),loglik=ll[1],loglik.se=ll[2])
} -> r_prof

Notice that we’ve changed the mif2 perturbations (rw.sd): we’ve removed the perturbation on the \(r\) parameter, since we want to hold this parameter fixed.

We add these points to our database:

estimates %>%
  rbind(r_prof) -> estimates

Next, we plot the likelihood profile. The following plot shows the top two estimates for each value of \(r\) with error bars showing ±2 s.e. and a loess smooth.

r_prof %>%
  group_by(r) %>%
  filter(rank(-loglik)<=2) %>%
  ungroup() %>%
  ggplot(aes(x=r,y=loglik,
    ymin=loglik-2*loglik.se,ymax=loglik+2*loglik.se))+
  geom_point()+
  geom_errorbar()+
  geom_smooth(method="loess",span=0.2)+
  scale_x_log10()

We see that the 95% likelihood ratio test confidence interval (CI) appears to be one-sided: the CI is roughly \(r>0.75\).

If we plot the profile trace of \(\sigma\), we see that, as we increase \(r\), we have to increase the intensity of the environmental stochasticity to maintain a good fit. Why is this?

r_prof %>%
  group_by(r) %>%
  filter(rank(-loglik)<=2) %>%
  ungroup() %>%
  ggplot(aes(x=r,y=sigma))+
  geom_point()+
  geom_smooth(method="loess",span=0.2)+
  scale_x_log10()+
  labs(y=expression(sigma))


Exercise

Construct a likelihood profile for the \(K\) parameter. Plot the profile and profile traces. Comment on your findings.


Model criticism

Estimating model parameters by fitting the model to data is typically only a step in the process of trying to understand the processes that generated the data. The next step involves trying to understand how and why the model fits the data the way it does, whether it fits it well, and what scope for improvement there might be. pomp2 provides a number of tools to facilitate answering these questions through interaction with a fitted model.

Simulation of the fitted model

Ultimately, since the model is viewed, at least hypothetically, as the process that generated the data, simulation of the fitted model is a central tool we have for model criticism. Let’s plot the data and several simulated realizations of the model process on the same axes.

r_prof %>% 
  filter(loglik==max(loglik)) -> mle

mlepomp <- as(mifs[[1]],"pomp")
coef(mlepomp) <- mle

mlepomp %>%
  simulate(nsim=8,format="data.frame",include.data=TRUE) %>%
  ggplot(mapping=aes(x=year,y=pop,group=.id,alpha=(.id=="data")))+
  scale_alpha_manual(values=c(`TRUE`=1,`FALSE`=0.2),
    labels=c(`FALSE`="simulation",`TRUE`="data"))+
  labs(alpha="")+
  geom_line()+
  theme_bw()

The first lines above simply extract the maximum likelihood estimates (mle) from our profle computation. The next pair of lines plug these MLE parameters into a ‘pomp’ object (mlepomp) containing the model and the data. The last set of lines do the simulation and the plotting.

Although it is clear from these plots that the estimated model has more variability and is thus able to explain the data better, it can be hard to read much from spaghetti plots such as this. It’s almost always a good idea to plot the data together with several simulated realizations in order to help assess how similar the two are.

mlepomp %>%
  simulate(nsim=11,format="data.frame",include.data=TRUE) %>%
  ggplot(mapping=aes(x=year,y=pop,group=.id,color=(.id=="data")))+
  scale_color_manual(values=c(`TRUE`="black",`FALSE`="grey50"),
    labels=c(`FALSE`="simulation",`TRUE`="data"))+
  labs(color="")+
  geom_line()+
  facet_wrap(~.id)+
  theme_bw()

Model checking with probes

Visual comparison of simulations and data is always a good idea. An indication that the data are not a plausible realization of the model is evidence for lack of fit. In particular, if we have any set of summary statistics, or probes, we can apply them to both simulated and actual data. The probe function facilitates this comparison. Let’s perform this operation using several of the summary statistics provided with pomp2: we’ll use the mean, several quantiles, and the autocorrelation at lags 1 and 3.

mlepomp %>%
  probe(nsim=200,probes=list(
    mean=probe.mean("pop"),
    q=probe.quantile("pop",probs=c(0.05,0.25,0.5,0.75,0.95)),
    probe.acf("pop",lags=c(1,3),type="corr",transform=log)
  )) -> vp_probe

vp_probe
## <object of class 'probed_pomp'>

For ‘probed_pomp’ object, there are summary and plot methods. There is also an as.data.frame method.

summary(vp_probe)
## $coef
##            r            K        sigma          N_0            b 
##   18.4919836  211.2619239    1.4493337  146.4114716    1.0000000 
##       loglik    loglik.se 
## -141.2953243    0.1565764 
## 
## $nsim
## [1] 200
## 
## $quantiles
##   mean   q.5%  q.25%  q.50%  q.75%  q.95% acf[1] acf[3] 
##  0.460  0.490  0.720  0.290  0.300  0.755  0.660  0.830 
## 
## $pvals
##      mean      q.5%     q.25%     q.50%     q.75%     q.95%    acf[1] 
## 0.9253731 0.9850746 0.5572139 0.5870647 0.6069652 0.4875622 0.6865672 
##    acf[3] 
## 0.3482587 
## 
## $synth.loglik
## [1] -18.94921

Evidently, summary returns a list with several elements. The quantiles element contains, for each probe, what fraction of the nsim simulations had probe values below the value of the probe applied to the data. The pvals element contains \(P\)-values associated with the two-sided test of the hypothesis that the data were generated by the model.

plot(vp_probe)

The plot depicts the multivariate distribution of the probes under the model, with the data-values superimposed. On the diagonal, we see the marginal distributions of the individual probes, represented as histograms, with the vertical line signifying the value of the corresponding probe on the data. Above the diagonal, the scatterplots show the pairwise distributions of probes and the crosshairs, the corresponding data-values. Below the diagonal, the panels contains the pairwise correlations among the simulated probes.

Next steps

To this point, we’ve seen how to implement POMP models, simulate them, to compute and maximize likelihoods, and perform certain kinds of diagnostic checks. The pomp website contains more documentation, including the full package manual, and a variety of tutorials and short courses. The package itself contains a number of built-in examples and datasets that can be explored.

pomp2 provides a large toolbox of different inference methods, only a few of which have been explored here. In particular, the package provides other methods for parameter estimation, both in the frequentist and Bayesian modes. See for example (abc, pmcmc, probe.match, spect.match, nlf, enkf, bsmc2). It also provides a variety of tools for model checking (spect, nlf). It is frequently the case that an approach that makes use of more than one approach has advantages over more “purist” approaches: the main goal of the package is to facilitate effective inference by bringing a variety of tools, with complementary strengths and weaknesses, to the user in a common format.

Although the goal of this document has been to introduce the beginner to the package through a display of the pomp2 toolbox in the context of a rudimentary and incomplete data analysis of a short time series with toy models, it is important to realize that these tools have proven their utility on some extremely challenging problems, including some for which other existing methods are either less efficient or entirely infeasible. The bibliography has links to peer-reviewed publications that have used these methods.

References

Arulampalam, M. S., S. Maskell, N. Gordon, and T. Clapp. 2002. A tutorial on particle filters for online nonlinear, non-Gaussian Bayesian tracking. IEEE Transactions on Signal Processing 50:174–188.

Ionides, E. L., C. Bretó, and A. A. King. 2006. Inference for nonlinear dynamical systems. Proceedings of the National Academy of Sciences of the U.S.A. 103:18438–18443.

Ionides, E. L., D. Nguyen, Y. Atchadé, S. Stoev, and A. A. King. 2015. Inference for dynamic and latent variable models via iterated, perturbed Bayes maps. Proceedings of the National Academy of Sciences of the U.S.A. 112:719–724.

McCleery, R. H., and C. M. Perrins. 1991. Effects of predation on the numbers of great tits Parus major. Pages 129–147 in Bird population studies. relevence to conservation and management. Oxford University Press, Oxford.

Ricker, W. E. 1954. Stock and recruitment. Journal of the Fisheries Research Board of Canada 11:559–623.